const UnionFind = require("../template")

function testUnionFindBasic() {
  const uf = new UnionFind(5)

  console.log(
    "1. 基本合并测试:",
    (() => {
      uf.union(0, 1)
      uf.union(2, 3)
      return uf.connected(0, 1) === true &&
             uf.connected(2, 3) === true &&
             uf.connected(0, 2) === false
    })()
  )

  console.log(
    "2. 连通分量数量测试:",
    (() => {
      return uf.getComponentsCount() === 3 // 0,1 | 2,3 | 4
    })()
  )

  console.log(
    "3. 集合大小测试:",
    (() => {
      return uf.getComponentSize(0) === 2 &&
             uf.getComponentSize(2) === 2 &&
             uf.getComponentSize(4) === 1
    })()
  )

  console.log(
    "4. 跨集合合并测试:",
    (() => {
      uf.union(1, 2)
      return uf.connected(0, 3) === true &&
             uf.getComponentsCount() === 2
    })()
  )

  console.log(
    "5. 最大连通分量测试:",
    (() => {
      return uf.getLargestComponentSize() === 4
    })()
  )

  console.log(
    "6. this上下文并查集测试:",
    (() => {
      const ufObj = {
        multiplier: 2,
        processUnionFind: function(n) {
          const uf = new UnionFind(n)
          uf.union(0, 1)
          return uf.getComponentsCount() * this.multiplier
        }
      }
      return ufObj.processUnionFind(3) === 4 // (3-1)*2=4
    })()
  )

  console.log(
    "7. 复杂对象this绑定并查集测试:",
    (() => {
      const ufConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(n) {
          const uf = new UnionFind(n)
          uf.union(0, 1)
          return uf.getLargestComponentSize() >= this.config.threshold
        }
      }
      return ufConfigObj.processWithConfig(3) === true
    })()
  )

  console.log(
    "8. 并查集一致性测试:",
    (() => {
      const consistencyUf = new UnionFind(4)
      consistencyUf.union(0, 1)
      consistencyUf.union(2, 3)
      const count1 = consistencyUf.getComponentsCount()
      consistencyUf.union(1, 2)
      const count2 = consistencyUf.getComponentsCount()
      return count1 === 2 && count2 === 1
    })()
  )

  console.log(
    "9. 并查集边界测试:",
    (() => {
      const boundaryUf = new UnionFind(1)
      return boundaryUf.getComponentsCount() === 1 &&
             boundaryUf.getComponentSize(0) === 1 &&
             boundaryUf.getLargestComponentSize() === 1
    })()
  )

  console.log(
    "10. 并查集性能测试:",
    (() => {
      const perfUf = new UnionFind(1000)
      const start = performance.now()
      for (let i = 0; i < 1000; i += 2) {
        perfUf.union(i, i + 1)
      }
      const end = performance.now()
      return perfUf.getComponentsCount() === 500 && (end - start) < 1000
    })()
  )
}

testUnionFindBasic()
